import 'package:banyou_app/app/config/colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

//自定义AppBar
class AppBarWidget extends StatefulWidget implements PreferredSizeWidget {
  //标题
  final String? titleStr;

  //点击左边回调 不为null 则自己处理返回方法
  final VoidCallback? leadingCallback;
  final List<Widget>? actions;
  final Widget? leading;

  final Widget? titleWidget;

  //背景颜色
  final Color backgroundColor;

  //文字颜色
  final Color? titleColor;

  //是否显示左边按钮
  final bool showLeading;

  final PreferredSizeWidget? bottom;

  const AppBarWidget(
      {this.titleStr,
      this.titleWidget,
      this.leadingCallback,
      this.actions,
      this.backgroundColor = ColorConfig.bg_color,
      this.titleColor,
      this.leading,
      this.showLeading = true,
      this.bottom})
      : super();

  @override
  _AppBarWidgetState createState() => _AppBarWidgetState();

  @override
  Size get preferredSize =>
      Size.fromHeight(44 + (bottom?.preferredSize.height ?? 0.0));
}

class _AppBarWidgetState extends State<AppBarWidget> {
  @override
  Widget build(BuildContext context) {
    return AppBar(
      backgroundColor: widget.backgroundColor,
      title: widget.titleWidget == null
          ? Text(widget.titleStr ?? "",
              style: TextStyle(color: ColorConfig.text_label, fontSize: 18))
          : widget.titleWidget,
      centerTitle: true,
      elevation: 0,
      systemOverlayStyle: SystemUiOverlayStyle.dark,
      leading: widget.leading ??
          Offstage(
            offstage: widget.showLeading == false,
            child: GestureDetector(
              child: Center(
                child: Container(
                  padding: EdgeInsets.all(5.0),
                  child: Image.asset('assets/images/pop.png',width: 26,),
                  // child: Icon(
                  //   Icons.arrow_back_ios,
                  //   size: 25,
                  //   color: ColorConfig.text_label,
                  // ),
                ),
              ),
              onTap: () {
                if (widget.leadingCallback == null) {
                  Get.back();
                } else {
                  widget.leadingCallback!();
                }
              },
            ),
          ),
      actions: widget.actions,
      bottom: widget.bottom,
      leadingWidth: widget.leading == null ? 56 : 150,
    );
  }
}
